package com.ruoyi.web.controller.document;



import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.DocFile;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.framework.config.ServerConfig;
import com.ruoyi.system.service.DocAccService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * 通用请求处理
 *
 * @author ruoyi
 */
@Api(tags = "项目附件管理")
@RestController
@RequestMapping("/doc/acc")
public class DocAccController extends BaseController{

    @Autowired
    private DocAccService docAccService;

    @Autowired
    private ServerConfig serverConfig;


    /**
     * 获取文档信息详细信息
     */
    @ApiOperation(value = "获取项目文档详细信息")
    @PreAuthorize("@ss.hasPermi('document:message:getInfo')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(docAccService.selectDocFileByFileId(id));
    }


    /***
     * 新增文件信息
     * @param
     * @return
     */
    @ApiOperation(value = "新增文件信息")
    @PreAuthorize("@ss.hasPermi('document:message:add')")
    @Log(title = "新增文件信息", businessType = BusinessType.DELETE)
    @PostMapping("/add")
    public AjaxResult insertAccUpdate(@RequestBody DocFile docFile) {
        docFile.setFileLoader(getUsername());
        return toAjax(docAccService.insertAccUpdate(docFile));
    }


    /***
     * 修改文件信息
     * @param
     * @return
     */
    @ApiOperation(value = "修改文件信息")
    @PreAuthorize("@ss.hasPermi('document:message:update')")
    @Log(title = "修改文件信息", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    public AjaxResult updateAccUpdate(@RequestBody DocFile docFile) {
        docFile.setFileLoader(getUsername());
        return toAjax(docAccService.updateAccUpdate(docFile));
    }


    /***
     * 逻辑删除项目文档信息
     * @param fileId
     * @return
     */
    @ApiOperation(value = "逻辑删除项目文档信息")
    @Log(title = "逻辑删除项目文档信息", businessType = BusinessType.DELETE)
    @PreAuthorize("@ss.hasPermi('document:message:delete')")
    @DeleteMapping("{fileId}")
    public AjaxResult delete(@PathVariable("fileId") Long[] fileId) {
        return toAjax(docAccService.deleteByIds(fileId));
    }


    /***
     * 修改项目文档信息状态:审核通过
     * @param fileId
     * @return
     */
    @ApiOperation(value = "修改项目文档信息状态")
    @Log(title = "修改项目文档信息状态", businessType = BusinessType.UPDATE)
    @PreAuthorize("@ss.hasPermi('document:message:audit')")
    @PutMapping(value = "/state/yes/{fileId}")
    public AjaxResult updateState(@PathVariable Long fileId) {
        return toAjax(docAccService.updateYesState(fileId));
    }


    /***
     * 审核项目文档信息状态:审核不通过
     * @param fileId
     * @return
     */
    @ApiOperation(value = "修改项目文档信息状态")
    @Log(title = "修改项目文档信息状态", businessType = BusinessType.UPDATE)
    @PreAuthorize("@ss.hasPermi('document:message:noaudit')")
    @PutMapping("state/no/{fileId}")
    public AjaxResult updateNoState(@PathVariable Long fileId) {
        return toAjax(docAccService.updateNoState(fileId));
    }


    /**
     * 上传请求
     */
    @ApiOperation(value = "实现文件上传")
    @Log(title = "文件上传", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('document:message:upload')")
    @PostMapping("upload")
    @ResponseBody
    public AjaxResult uploadFile(MultipartFile file) throws Exception {
        try {
            // 上传文件路径
            String filePath = RuoYiConfig.getUploadPath();
            // 上传并返回文件名称
            String fileName = FileUploadUtils.upload(filePath, file);
            String url = serverConfig.getUrl() + fileName;
            AjaxResult ajax = AjaxResult.success();
            String originalFilename = file.getOriginalFilename();
            DocFile docFile = new DocFile();
            docFile.setUrl(url);
            docFile.setFilePath(filePath);
            docFile.setFileName(originalFilename);
            docFile.setFileUnique(fileName);
            Long fileId = docAccService.insertPathName(docFile);
            ajax.put("fileId",fileId);
            ajax.put("fileName", originalFilename);
            ajax.put("url", url);
            ajax.put("filePath", filePath);
            ajax.put("fileUnique", fileName);
            return ajax;
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }
}
